import { defineStore } from "pinia";
import {
  getPatientList,
  getCmUserById,
  getAppointmentList,
} from "../api/user.api";
import type {
  PatientRes,
  UserAppointmentRes,
  UserRes,
} from "../../types/user.type";
import { setClinicId } from "../utils/auth.util";
import { getClinicInfo } from "../api/common.api";
interface UserStoreState {
  patientList: PatientRes[];
  userInfo: UserRes | null;
  // 当前就诊人
  currentPatient: PatientRes | null;
  appointmentList: UserAppointmentRes[];
  clinicInfo: {
    clinicId: number; //诊所ID
    clinicName: string; //诊所名
    logo: string; //诊所logo
    license: string; //营业执照
    licenseUrl: string; //营业执照图片
    province: string; //省代码
    city: string; //市代码
    county: string; //县代码
    address: string; //地址
  } | null;
}

export const useUserStore = defineStore("userStore", {
  state: (): UserStoreState => {
    return {
      patientList: [],
      userInfo: null,
      currentPatient: null,
      appointmentList: [],
      clinicInfo: null,
    };
  },
  actions: {
    // 获取诊所信息
    async getClinicInfoApi() {
      const res = await getClinicInfo();
      if (res.code === "200") {
        this.clinicInfo = res.data ?? null;
      }
    },
    // 获取用户信息
    async getCmUserByIdApi() {
      const res = await getCmUserById();

      if (res.code === "200") {
        this.userInfo = res.data;
        setClinicId(res.data.clinicId.toString());
      }
    },
    // 获取就诊人列表
    async getPatientListApi() {
      const res = await getPatientList();

      if (res.code === "200") {
        this.patientList = res.data;
      }
    },
    // 获取我的预约列表
    async getAppointmentListApi(params: {
      regStatus?: number;
      pageNo?: number;
      pageSize?: number;
    }) {
      const res = await getAppointmentList(params);

      if (res.code === "200") {
        this.appointmentList = res.data;
      }
    },
  },
});
